package pt.fct.mpc.lastfm.database;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import android.util.Log;

public class LastfmDatabaseManager
{
	private static final TimeUnit SYNCH_UNIT = TimeUnit.SECONDS;
	private static final long SYNCH_PERIOD = 3600; // one hour
	private static LastfmDatabaseManager instance = null;
	
	private final ScheduledExecutorService scheduler =
		Executors.newScheduledThreadPool(1);
	
	private ScheduledFuture<?> syncherHandle = null;
	
	private LastfmDatabaseManager()
	{
	}
	
	static public LastfmDatabaseManager getInstance()
	{
		if (instance == null)
			instance = new LastfmDatabaseManager();
		
		return instance;
	}
	
	public void databaseScheduleSync()
	{
		final Runnable syncher = new Runnable()
		{
			@Override
			public void run()
			{
				databaseSync();
			}

		};
		
		syncherHandle =
			scheduler.scheduleAtFixedRate(syncher, 0, SYNCH_PERIOD, SYNCH_UNIT);
	}
	
	public void databaseStopSync()
	{
		// stop database synch'ing, but wait until it stops running.
		this.syncherHandle.cancel(false);
	}

	private void databaseSync()
	{
		Log.d("MPC", "Synch'ing database");
		// get top albums
		// get top artists
		// get top tracks
		// get events
	}
}
